package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.comm.QiniuOssUtils;
import com.softeem.music_sys.entity.Admin;
import com.softeem.music_sys.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * 管理员控制器
 */
@Tag(name = "管理员接口", description = "管理员相关操作:登录、注册、信息管理等")
@RestController
@RequestMapping("/admin")
public class AdminController {
    
    @Autowired
    private AdminService adminService;

    @Operation(summary = "管理员注册", description = "管理员注册，提供管理员信息")
    @PostMapping("/register")
    public Result register(@RequestBody Admin admin) {
        return adminService.registerAdmin(admin);
    }

    @Operation(summary = "管理员登录", description = "管理员登录，提供用户名和密码")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "password", description = "密码")
    })
    @PostMapping("/login")
    public Result login(@RequestBody Admin loginAdmin) {
        return adminService.loginAdmin(loginAdmin.getUsername(), loginAdmin.getPassword());
    }

    @Operation(summary = "获取管理员信息", description = "根据ID获取管理员详细信息")
    @GetMapping("/{id}")
    public Result getAdminById(@PathVariable Integer id) {
        return adminService.getAdminById(id);
    }

    @Operation(summary = "更新管理员信息", description = "更新管理员信息")
    @PutMapping
    public Result update(@RequestBody Admin admin) {
        return adminService.updateAdmin(admin);
    }

    @Operation(summary = "删除管理员", description = "根据ID删除管理员")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return adminService.deleteAdmin(id);
    }

    @Operation(summary = "获取管理员列表", description = "分页获取管理员列表")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer limit) {
        return adminService.getAdminList(page, limit);
    }

    @Operation(summary = "搜索管理员", description = "根据用户名搜索管理员")
    @GetMapping("/search")
    public Result search(@RequestParam String username,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer limit) {
        return adminService.searchAdmin(username, page, limit);
    }

    @Operation(summary = "禁用/启用管理员", description = "修改管理员状态")
    @Parameters({
            @Parameter(name = "id", description = "管理员ID"),
            @Parameter(name = "status", description = "状态 1-启用 0-禁用")
    })
    @PutMapping("/status/{id}")
    public Result updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        return adminService.updateAdminStatus(id, status);
    }

    @Operation(summary = "上传音乐文件", description = "上传音乐文件到七牛云存储")
    @PostMapping("/music/upload")
    public Result uploadMusic(@RequestParam("file") MultipartFile file) {
        return adminService.uploadMusicFile(file);
    }
}